package com.blakebarrett.business
{
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	
	public class VersionService
	{
		public function VersionService()
		{
			versionFetcher = new HTTPService();
			versionFetcher.url = "http://www.blakebarrett.net/versions/stereogramstudio.xml"; 
			versionFetcher.addEventListener(ResultEvent.RESULT, onResult );
			versionFetcher.addEventListener(FaultEvent.FAULT, function ( f : FaultEvent ):void {} );
			versionFetcher.send();
		}
		
		public var versionFetcher : HTTPService; 
		
		public const CURRENT_VERSION : String = "1.5";
		public var latestVersion : String;
		
		private function onResult ( e : ResultEvent ) : void {
			latestVersion = e.result.currentVersion.toString();
			evaluateVersions();
		}
		
		public function get hasCurrentVersion () : Boolean {
			// just incase you are running out of the trunk
			return (CURRENT_VERSION >= latestVersion);
		}
		
		private function evaluateVersions () : void {
			if ( !hasCurrentVersion ) {
				Alert.show("A newer version: " + latestVersion + " is available for download.\nhttp://code.google.com/p/stereogram-studio");
			}
		}
		
	}
}